package com.resourcestore.action;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.resourcestore.utils.CommonUtil;
import com.resourcestore.action.BaseAction;
import com.resourcestore.dao.*;
import com.resourcestore.model.*;
import com.resourcestore.utils.tag.Pagination;

public class TeacherMainAction extends BaseAction {
	private Term term;
	private List<Term> listTerm;
	private TermDao termDao = new TermDao();
	private CourseTeaClassFK courseTeaClassFK;
	private List<CourseTeaClassFK> listCourseTeaClassFK;
	private CourseTeaClassFKDao courseTeaClassFkDao = new CourseTeaClassFKDao();
	private Users users;
	private UsersDao usersDao = new UsersDao();
	private Announce announce;
	private List<Announce> listAnnounce;
	private AnnounceDao announceDao = new AnnounceDao();
	private CourseFileSrcCatePublicDao courseFileSrcCatePublicDao = new CourseFileSrcCatePublicDao();
	private List<CourseFileSrcCatePublic> listCourseFileSrcCatePublic;
	private CourseFileSrcCatePublic courseFileSrcCatePublic;
	private CourseFileSrcCatePrivate courseFileSrcCatePrivate;
	private List<CourseFileSrcCatePrivate> listCourseFileSrcCatePrivate;
	private CourseFileSrcCatePrivateDao courseFileSrcCatePrivateDao = new CourseFileSrcCatePrivateDao();

	/**
	 * 首页内容
	 * 
	 */
	public String teacherIndexContent() {
		listAnnounce = announceDao.selectAnnounce();

		for (int i = 0; i < listAnnounce.size(); i++) {
			try {
				listAnnounce.get(i).setGmtCreatedString(
						CommonUtil.getSysTimeFormatString(listAnnounce.get(i)
								.getGmtCreated()));
				listAnnounce.get(i).setGmtModifiedString(
						CommonUtil.getSysTimeFormatString(listAnnounce.get(i)
								.getGmtModified()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return newpath("/TeacherMain/teacherIndexContent.jsp");
	}

	/**
	 * 功能导航
	 * 
	 */
	public String teacherTree() {

		//查询学期
		Term term = new Term();
		// int year = Calendar.getInstance().get(Calendar.YEAR);
		// term.setBelongYear(year);
		listTerm = termDao.selectTerm();

		//查询教师负责的课程
		CourseTeaClassFK courseTeaClassFK = new CourseTeaClassFK();
		Map session = ActionContext.getContext().getSession();
		String userName = "";
		userName = (String) session.get("userName");
		if (CommonUtil.isNotNullOrNotEqualsEmpty(userName)) {
			courseTeaClassFK.setTeacherUsername(userName);
		}
		listCourseTeaClassFK = courseTeaClassFkDao
				.selectCourseTeaClassFKByTUN(courseTeaClassFK);

		listCourseFileSrcCatePublic = new ArrayList<CourseFileSrcCatePublic>();
		listCourseFileSrcCatePrivate = new ArrayList<CourseFileSrcCatePrivate>();

		for (int i = 0; i < listCourseTeaClassFK.size(); i++) {

			// 预置课程公共文件分类
			CourseFileSrcCatePublic cfscIsPreTemp = new CourseFileSrcCatePublic();
			cfscIsPreTemp.setIsPreCourseCate(1);
			List<CourseFileSrcCatePublic> listCFSCIsPreTemp = courseFileSrcCatePublicDao
					.selectCourseFileSrcCatePublicByIsPreAndCourseId(cfscIsPreTemp);
			for (int k = 0; k < listCFSCIsPreTemp.size(); k++) {
				listCFSCIsPreTemp.get(k).setTermId(
						listCourseTeaClassFK.get(i).getTermId());
				listCFSCIsPreTemp.get(k).setCourseId(
						listCourseTeaClassFK.get(i).getCourseId());
				if (listCFSCIsPreTemp.get(k).getCourseFileCateParentId() == -1) {
					listCFSCIsPreTemp.get(k).setTermIdAndCourseIdString(
							"97"
									+ String.valueOf(listCourseTeaClassFK
											.get(i).getTermId())
									+ String.valueOf(listCourseTeaClassFK
											.get(i).getCourseId()));
					listCFSCIsPreTemp.get(k).setCourseFileCateParentIdString(
							null);
				} else {
					listCFSCIsPreTemp.get(k).setTermIdAndCourseIdString(
							"92"
									+ String.valueOf(listCourseTeaClassFK
											.get(i).getTermId())
									+ String.valueOf(listCourseTeaClassFK
											.get(i).getCourseId()));
					listCFSCIsPreTemp.get(k).setCourseFileCateParentIdString(
							String.valueOf(listCFSCIsPreTemp.get(k)
									.getCourseFileCateParentId()));
				}
				listCFSCIsPreTemp.get(k).setCourseFileCateName(
						"<span class='isPreCate'>"
								+ listCFSCIsPreTemp.get(k)
										.getCourseFileCateName() + "</span>");
			}
			listCourseFileSrcCatePublic.addAll(listCFSCIsPreTemp);

			// 非预置课程公共文件分类
			cfscIsPreTemp = new CourseFileSrcCatePublic();
			cfscIsPreTemp.setIsPreCourseCate(0);
			cfscIsPreTemp.setCourseId(listCourseTeaClassFK.get(i).getCourseId());
			List<CourseFileSrcCatePublic> listCFSCIsNotPreTemp = courseFileSrcCatePublicDao
					.selectCourseFileSrcCatePublicByIsPreAndCourseId(cfscIsPreTemp);
			for (int k = 0; k < listCFSCIsNotPreTemp.size(); k++) {
				listCFSCIsNotPreTemp.get(k).setTermId(
						listCourseTeaClassFK.get(i).getTermId());
				if (listCFSCIsNotPreTemp.get(k).getCourseFileCateParentId() == -1) {
					listCFSCIsNotPreTemp.get(k).setTermIdAndCourseIdString(
							"97"
									+ String.valueOf(listCourseTeaClassFK
											.get(i).getTermId())
									+ String.valueOf(listCourseTeaClassFK
											.get(i).getCourseId()));
					listCFSCIsNotPreTemp.get(k)
							.setCourseFileCateParentIdString(null);
				} else {
					listCFSCIsNotPreTemp.get(k).setTermIdAndCourseIdString(
							"92"
									+ String.valueOf(listCourseTeaClassFK
											.get(i).getTermId())
									+ String.valueOf(listCourseTeaClassFK
											.get(i).getCourseId()));
					listCFSCIsNotPreTemp.get(k)
							.setCourseFileCateParentIdString(
									String.valueOf(listCFSCIsNotPreTemp.get(k)
											.getCourseFileCateParentId()));
				}
				listCFSCIsNotPreTemp.get(k).setCourseFileCateName(
						"<span class='isNotPreCate'>"
								+ listCFSCIsNotPreTemp.get(k)
										.getCourseFileCateName() + "</span>");
			}
			listCourseFileSrcCatePublic.addAll(listCFSCIsNotPreTemp);

			//预置课程私有文件分类
			CourseFileSrcCatePrivate cfscPrivateIsPreTemp = new CourseFileSrcCatePrivate();
			cfscPrivateIsPreTemp.setIsPreCourseCate(1);
			List<CourseFileSrcCatePrivate> listCFSCPrivateIsPreTemp = courseFileSrcCatePrivateDao.selectCourseFileSrcCatePrivateByIsPre(cfscPrivateIsPreTemp);
			for (int k = 0; k < listCFSCPrivateIsPreTemp.size(); k++) {
				listCFSCPrivateIsPreTemp.get(k).setTermId(
						listCourseTeaClassFK.get(i).getTermId());
				listCFSCPrivateIsPreTemp.get(k).setCourseId(
						listCourseTeaClassFK.get(i).getCourseId());
				listCFSCPrivateIsPreTemp.get(k).setTeacherUsername(
						listCourseTeaClassFK.get(i).getTeacherUsername());
				if (listCFSCPrivateIsPreTemp.get(k).getCourseFileCateParentId() == -1) {
					listCFSCPrivateIsPreTemp.get(k).setTermIdAndCourseIdString(
							"96"
									+ String.valueOf(listCourseTeaClassFK
											.get(i).getTermId())
									+ String.valueOf(listCourseTeaClassFK
											.get(i).getCourseId()));
					listCFSCPrivateIsPreTemp.get(k).setCourseFileCateParentIdString(
							null);
				} else {
					listCFSCPrivateIsPreTemp.get(k).setTermIdAndCourseIdString(
							"91"
									+ String.valueOf(listCourseTeaClassFK
											.get(i).getTermId())
									+ String.valueOf(listCourseTeaClassFK
											.get(i).getCourseId()));
					listCFSCPrivateIsPreTemp.get(k).setCourseFileCateParentIdString(
							String.valueOf(listCFSCPrivateIsPreTemp.get(k)
									.getCourseFileCateParentId()));
				}
				listCFSCPrivateIsPreTemp.get(k).setCourseFileCateName(
						"<span class='isPreCate'>"
								+ listCFSCPrivateIsPreTemp.get(k)
										.getCourseFileCateName() + "</span>");
			}
			listCourseFileSrcCatePrivate.addAll(listCFSCPrivateIsPreTemp);

			// 非预置课程私有文件分类
			cfscPrivateIsPreTemp = new CourseFileSrcCatePrivate();
			cfscPrivateIsPreTemp.setIsPreCourseCate(0);
			//cfscPrivateIsPreTemp.setTermId(listCourseTeaClassFK.get(i).getTermId());
			cfscPrivateIsPreTemp.setCourseId(listCourseTeaClassFK.get(i).getCourseId());
			//cfscPrivateIsPreTemp.setTeacherUsername(listCourseTeaClassFK.get(i).getTeacherUsername());
			List<CourseFileSrcCatePrivate> listCFSCPrivateIsNotPreTemp = courseFileSrcCatePrivateDao
					.selectCourseFileSrcCatePrivateByIsPreAndTrdId(cfscPrivateIsPreTemp);
			for (int k = 0; k < listCFSCPrivateIsNotPreTemp.size(); k++) {
				listCFSCPrivateIsNotPreTemp.get(k).setTermId(
						listCourseTeaClassFK.get(i).getTermId());
				if (listCFSCPrivateIsNotPreTemp.get(k).getCourseFileCateParentId() == -1) {
					listCFSCPrivateIsNotPreTemp.get(k).setTermIdAndCourseIdString(
							"96"
									+ String.valueOf(listCourseTeaClassFK
											.get(i).getTermId())
									+ String.valueOf(listCourseTeaClassFK
											.get(i).getCourseId()));
					listCFSCPrivateIsNotPreTemp.get(k)
							.setCourseFileCateParentIdString(null);
				} else {
					listCFSCPrivateIsNotPreTemp.get(k).setTermIdAndCourseIdString(
							"91"
									+ String.valueOf(listCourseTeaClassFK
											.get(i).getTermId())
									+ String.valueOf(listCourseTeaClassFK
											.get(i).getCourseId()));
					listCFSCPrivateIsNotPreTemp.get(k)
							.setCourseFileCateParentIdString(
									String.valueOf(listCFSCPrivateIsNotPreTemp.get(k)
											.getCourseFileCateParentId()));
				}
				listCFSCPrivateIsNotPreTemp.get(k).setCourseFileCateName(
						"<span class='isNotPreCate'>"
								+ listCFSCPrivateIsNotPreTemp.get(k)
										.getCourseFileCateName() + "</span>");
			}
			listCourseFileSrcCatePrivate.addAll(listCFSCPrivateIsNotPreTemp);
		}

		return newpath("/TeacherMain/teacherTree.jsp");
	}

	public Term getTerm() {
		return term;
	}

	public void setTerm(Term term) {
		this.term = term;
	}

	public List<Term> getListTerm() {
		return listTerm;
	}

	public void setListTerm(List<Term> listTerm) {
		this.listTerm = listTerm;
	}

	public List<CourseTeaClassFK> getListCourseTeaClassFK() {
		return listCourseTeaClassFK;
	}

	public void setListCourseTeaClassFK(
			List<CourseTeaClassFK> listCourseTeaClassFK) {
		this.listCourseTeaClassFK = listCourseTeaClassFK;
	}

	public Users getUsers() {
		return users;
	}

	public void setUsers(Users users) {
		this.users = users;
	}

	public Announce getAnnounce() {
		return announce;
	}

	public void setAnnounce(Announce announce) {
		this.announce = announce;
	}

	public List<Announce> getListAnnounce() {
		return listAnnounce;
	}

	public void setListAnnounce(List<Announce> listAnnounce) {
		this.listAnnounce = listAnnounce;
	}

	public CourseTeaClassFK getCourseTeaClassFK() {
		return courseTeaClassFK;
	}

	public void setCourseTeaClassFK(CourseTeaClassFK courseTeaClassFK) {
		this.courseTeaClassFK = courseTeaClassFK;
	}

	public List<CourseFileSrcCatePublic> getListCourseFileSrcCatePublic() {
		return listCourseFileSrcCatePublic;
	}

	public void setListCourseFileSrcCatePublic(
			List<CourseFileSrcCatePublic> listCourseFileSrcCatePublic) {
		this.listCourseFileSrcCatePublic = listCourseFileSrcCatePublic;
	}

	public CourseFileSrcCatePublic getCourseFileSrcCatePublic() {
		return courseFileSrcCatePublic;
	}

	public void setCourseFileSrcCatePublic(
			CourseFileSrcCatePublic courseFileSrcCatePublic) {
		this.courseFileSrcCatePublic = courseFileSrcCatePublic;
	}

	public CourseFileSrcCatePrivate getCourseFileSrcCatePrivate() {
		return courseFileSrcCatePrivate;
	}

	public void setCourseFileSrcCatePrivate(
			CourseFileSrcCatePrivate courseFileSrcCatePrivate) {
		this.courseFileSrcCatePrivate = courseFileSrcCatePrivate;
	}

	public List<CourseFileSrcCatePrivate> getListCourseFileSrcCatePrivate() {
		return listCourseFileSrcCatePrivate;
	}

	public void setListCourseFileSrcCatePrivate(
			List<CourseFileSrcCatePrivate> listCourseFileSrcCatePrivate) {
		this.listCourseFileSrcCatePrivate = listCourseFileSrcCatePrivate;
	}

}
